<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\ArticleModel;
use App\Model\CateModel;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $articleModel = new ArticleModel;
        $articles = $articleModel->getArts();
        
        return view('admin.article.index',['articles' => $articles]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $cateModel = new CateModel;
        $cates = $cateModel->getCates();
        
        return view('admin.article.add',['cates' => $cates]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        $articleModel = new ArticleModel;
        $res = $articleModel->insertArticle($data);

        if ( $res ) {
            return ['msg' => '添加成功' , 'status' => 'success'];
        } else {
            return ['msg' => '添加失败' , 'status' => 'fail'];
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $articleModel = new ArticleModel;
        $article = $articleModel->getArticle($id);
        $cateModel = new CateModel;
        $cates = $cateModel->getCates();
        return view('admin.article.edit',['article' => $article , 'cates' => $cates]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($request->input());
        $data = $request->except('_token' , '_method');
        $articleModel = new ArticleModel;
        $res = $articleModel->upArticle($id , $data);

        if ( $res ) {
            return ['msg' => '更新成功' , 'status' => 'success'];
        } else {
            return ['msg' => '更新失败' , 'status' => 'fail'];
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $articleModel = new ArticleModel;
        $res = $articleModel->delArticle($id);

        if ( $res ) {
            return ['msg' => '删除成功' , 'status' => 'success'];
        } else {
            return ['msg' => '删除失败' , 'status' => 'fail'];
        }
    }
}
